package com.malamute.core.auto.bean;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.malamute.core.auto.AutoBaseInf;

/**
 * 接口对象封装
 * 
 */
public class InterfaceWrapper implements AutoBaseInf {
	protected Logger log = LoggerFactory.getLogger(InterfaceWrapper.class);
	private String type = AutoBaseInf.TYPE_INTERFACE;// 接口类型

	private List<MethodBean> methodBeanList;// 方法名称
	// 实体对象
	private String modelName;// 实体对象名称
	private File modelFile;// 实体文件
	// 需要创建的属性
	private String fileRealPath;// 生成文件地址
	private String filePackage;// 生成文件包
	private String target = "test";// 创建目标地址

	private InterfaceWrapper() {

	}

	// 通过实体文件来构造
	public InterfaceWrapper(File modelFile) {
		this.modelFile = modelFile;
		String modelFileName = modelFile.getName();
		String modelFilePath = modelFile.getAbsolutePath();
		//
		this.modelName = modelFileName.substring(0, modelFileName.length()
				- FILE_END.length());
		log.info(modelFileName);
		String tmp = modelFilePath
				.substring(0, modelFilePath.lastIndexOf("\\"));
		// 文件地址
		fileRealPath = tmp.substring(0, tmp.lastIndexOf("\\")) + "\\"
				+ this.target;
		// 文件package
		filePackage = fileRealPath.substring(
				fileRealPath.indexOf("src") + "src".length() + 1).replace("\\",
				".");
		log.info("fileRealPath:" + fileRealPath);
		log.info("filePackage:" + filePackage);
	}

	@Override
	public void getFileNameByModelName() {

	}

	@Override
	public void createFile() throws IOException {
		File fileFolden = new File(fileRealPath);
		if (!fileFolden.exists()) {
			fileFolden.mkdirs();
		}
		File file = new File(fileFolden + "\\" + modelName + FILE_END);
		if (!file.exists()) {
			file.createNewFile();
			StringBuffer sb = new StringBuffer();
			sb.append(getStartPackage());
			sb.append(getFileClassStr());
			sb.append(getEnd());
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			bw.write(sb.toString());
			bw.close();
		}
	}

	public String getStartPackage() {
		// 包
		return "package " + filePackage + ";\n";
	}

	public String getEnd() {
		// 结束
		return "}";
	}

	public String getFileClassStr() {
		// 注释
		String author = "/**\n*@author AutoMaker  \n*/" + "\n";
		// 类型
		String classStr = "public " + this.type + " " + modelName + "{\n";
		return author + classStr;
	}

	public static void main(String[] args) throws IOException {
		File file = new File(
				"D:\\WorkSpace\\malamute-1.0\\.\\src\\com\\malamute\\system\\entity\\CafeInfo.java");
		InterfaceWrapper iw = new InterfaceWrapper(file);
		iw.createFile();

	}

}
